<?php

namespace App\Controller;

#Con
use App\Logic\ChatLogic;
use App\Middleware\AuthMiddleware;
use Exception;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;
use Throwable;

#[AutoController]
class ChatController extends AbstractController
{

    /**
     * 获取联系人
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function getContacts(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        $data = ChatLogic::getContact($userid, $input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 获取消息列表
     */
    #[Middleware(AuthMiddleware::class)]
    public function getMsgList(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $friendUserId = $this->request->input('friendUserId', 0);
        $lastMsgId = $this->request->input('lastMsgId', '');
        if (!$friendUserId) {
            return $this->apiReturn(400, '参数错误');
        }
        $data = ChatLogic::getMessageList($userid, $friendUserId, $lastMsgId, 20, true);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 发送消息
     */
    #[Middleware(AuthMiddleware::class)]
    public function sendMsg(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        if (!$input['toUserId'] || empty($input['msgType']) || empty($input['msgBody'])) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            ChatLogic::sendMsg($userid, $input['toUserId'], $input['msgType'], $input['msgBody']);
            return $this->apiReturn(200, '发送成功');
        } catch (Throwable $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 添加好友
     */
    #[Middleware(AuthMiddleware::class)]
    public function addFriend(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $targetUserId = $this->request->input('targetUserId', 0);
        $message = $this->request->input('message', '');
        try {
            ChatLogic::addFriend($userid, $targetUserId, $message);
            return $this->apiReturn(200, '申请发送成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 处理好友申请
     */
    #[Middleware(AuthMiddleware::class)]
    public function opFriendApply(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $applyId = $this->request->input('applyId', 0);
        $op = $this->request->input('op', 0);
        if (!$applyId || !$op) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            ChatLogic::opFriendApply($userid, $applyId, $op);
            return $this->apiReturn(200, '处理成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }
}